#include "parse.h"
#include "simulate.h"
#include "alg.h"
#include "timing.h"
#include "place.h"
#include "node.h"
#include <sstream>

#ifndef HUESEL2
#define HUESEL2
#endif

//#ifndef WRITERESMAP
//#define WRITERESMAP
//#endif

Circuit *C;
int MC_NUM;
int HeuTry;
vector<int> sim_vals;
extern bool formHybrid();

double criteria = 0.7;
int	main(int argc, char **argv)
{
        
	char *filename, *placename;
	int seed1,seed2,trace_size;
        HeuTry = 2;
	cout<<"parsing file............."<<endl;
	if (argc < 5) {
		cerr << "Usage: " << argv[0] << " bench_file" << " trace_size" << " seed1 seed2" <<endl;
		return 2;
	}
	else if (argc == 5) {
		filename = argv[1];
		trace_size = atoi(argv[2]);
		seed1 = atoi(argv[3]);
		seed2 = atoi(argv[4]);
	}
        else
        {
                cout<<"Wrong, retry!"<<endl;
                exit(1);
        }
	parse(filename);
        
        if(trace_size == 0)
        {
                cout<<"Cannot select nothing! Try again..."<<endl;
                exit(0);
        }
        
        for(int i=0; i<C->signalSize(); ++i)
                C->getSignal(i)->setIndex(i);
        
	for(int i=0; i<C->gateSize(); ++i)
                C->getGate(i)->setGateIndex(i);
	
        cout<<"placement"<<endl;
        Place *pl = new Place(C);

	Algorithm *alg = new Algorithm(C, pl, 0);
	
	cout<<"probablity computation......"<<endl;
	Simulate *sim = new Simulate(C);

        string tmp = filename;
	tmp += ".resMap";
        char *s = new char[strlen(tmp.c_str())];
        strcpy(s,tmp.c_str());
        
        MC_NUM = 100;
        
#ifndef HUESEL2
        
#ifdef WRITERESMAP
        ofstream out;
        cout<<tmp<<endl;
        //always open a clean and empty file
        out.open(s, ofstream::trunc);
        out.close();
        
        for(int i=0; i<C->flipflopSize(); ++i)
        {
                sim->clearTraced();
                C->getFlipflop(i)->getOutput(0)->setTraced(true);
                sim->addTraced(C->getFlipflop(i)->getOutput(0));
                cout<<"Flip flop "<< i<<"'s visibility is "<<sim->computVN(s)<<endl;
                C->getFlipflop(i)->getOutput(0)->setTraced(false);
        }
#endif        	
        sim->hueristicSelect(s, trace_size);        
#else
        string tmp1 = filename;
	tmp1 += ".prob";
        C->read_prob(&tmp1[0]);
        
        tmp += "2";
        ofstream out;
        cout<<tmp<<endl;
        //always open a clean and empty file
        out.open(tmp.c_str(), ofstream::trunc);
        out.close();
        
        //sim->hueristicSelect2(trace_size,tmp.c_str());        
#endif
        
        cout<<"Hueristic select ends............"<<endl;        
        return 0;
}
